package com.mallcloud.mall.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mallcloud.mall.admin.api.dto.UserInfo;
import com.mallcloud.mall.admin.api.entity.SysSocialDetails;
import com.mallcloud.mall.admin.api.entity.SysUser;
import com.mallcloud.mall.admin.handler.LoginHandler;
import com.mallcloud.mall.admin.mapper.SysSocialDetailsMapper;
import com.mallcloud.mall.admin.mapper.SysUserMapper;
import com.mallcloud.mall.admin.service.SysSocialDetailsService;
import com.mallcloud.mall.app.service.AppWxApiService;
import com.mallcloud.mall.app.service.AppWxUserService;
import com.mallcloud.mall.common.core.constant.CacheConstants;
import com.mallcloud.mall.common.security.util.SecurityUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 */
@Slf4j
@AllArgsConstructor
@Service
public class SysSocialDetailsServiceImpl extends ServiceImpl<SysSocialDetailsMapper, SysSocialDetails> implements SysSocialDetailsService {

	private final Map<String, LoginHandler> loginHandlerMap;
	private final CacheManager cacheManager;
	private final SysUserMapper sysUserMapper;
	private final AppWxApiService wxApiService;
	private final AppWxUserService appWxUserService;

	/**
	 * 绑定社交账号
	 *
	 * @param type type
	 * @param code code
	 * @return
	 */
	@Override
	public Boolean bindSocial(String type, String code) {
		LoginHandler loginHandler = loginHandlerMap.get(type);
		// 绑定逻辑
		String identify = loginHandler.identify(code);
		SysUser sysUser = sysUserMapper.selectById(SecurityUtils.getUser().getId());
		loginHandler.bind(sysUser, identify);

		// 更新缓存
		cacheManager.getCache(CacheConstants.USER_DETAILS).evict(sysUser.getUsername());
		return Boolean.TRUE;
	}

	/**
	 * 绑定手机号
	 *
	 * @param type      type
	 * @param phoneCode phoneCode
	 * @return
	 */
	@Override
	public Boolean bindPhone(String type, String phoneCode) {
		String phone = wxApiService.getUserPhoneNumber(phoneCode);

		LoginHandler loginHandler = loginHandlerMap.get(type);
		// 绑定逻辑
		SysUser updateUser = new SysUser();
		updateUser.setUserId(SecurityUtils.getUser().getId());
		loginHandler.bindPhone(updateUser, phone);

		// 更新缓存
		cacheManager.getCache(CacheConstants.USER_DETAILS).evict(SecurityUtils.getUser().getUsername());
		return Boolean.TRUE;
	}

	@Override
	public Boolean bindMall(String type) {
		SysUser sysUser = sysUserMapper.selectById(SecurityUtils.getUser().getId());
		String cardNo = appWxUserService.mallLoginOrRegister(sysUser);

		LoginHandler loginHandler = loginHandlerMap.get(type);
		// 绑定逻辑
		SysUser updateUser = new SysUser();
		updateUser.setUserId(sysUser.getUserId());
		loginHandler.bindCardNo(updateUser, cardNo);

		// 更新缓存
		cacheManager.getCache(CacheConstants.USER_DETAILS).evict(sysUser.getUsername());
		return Boolean.TRUE;
	}

	@Override
	public Boolean bindPhoneMall(String type, String phoneCode) {
		this.bindPhone(type, phoneCode);
		this.bindMall(type);
		return Boolean.TRUE;
	}

	/**
	 * 根据入参查询用户信息
	 *
	 * @param inStr TYPE@code
	 * @return
	 */
	@Override
	public UserInfo getUserInfo(String inStr) {
		String[] inStrs = inStr.split(StringPool.AT);
		String type = inStrs[0];
		String loginStr = inStr.substring(type.length() + 1);
		return loginHandlerMap.get(type).handle(loginStr);
	}
}
